package com.fjb.controller.category;


import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.enums.brand.BrandStatus;
import com.fjb.common.enums.product.CategoryStatus;
import com.fjb.common.global.GlobalConfig;
import com.fjb.pojo.brand.BrandInfo;
import com.fjb.pojo.brand.vo.BrandInfoVo;
import com.fjb.pojo.product.ProductCategory;
import com.fjb.pojo.product.query.ProductCategoryQuery;
import com.fjb.pojo.product.vo.ProductCategoryVo;
import com.fjb.service.brand.BrandInfoService;
import com.fjb.service.product.ProductCategoryService;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author hemiao
 * @since 2019-12-22
 */
@Controller
@RequestMapping("/category")
public class ProductCategoryController {
	
	@Autowired
	private ProductCategoryService productCategoryService;
	@Autowired
	private BrandInfoService brandInfoService;
	
	/**
	 * @Description:产品分类列表
	 * @param request
	 * @return
	 * JsonResult<List<BrandInfoVo>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月28日 下午7:44:51
	 */
	@RequestMapping(value="/selectAllList")
	@ResponseBody
	public JsonResult<List<ProductCategoryVo>> selectAllList(HttpServletRequest request){
		ProductCategoryQuery categoryQuery = new ProductCategoryQuery();
		categoryQuery.setCategoryStatus(CategoryStatus.STATUS_1.getCode());
		List<ProductCategoryVo> infoList = productCategoryService.selectList(categoryQuery);
		return new JsonResult<List<ProductCategoryVo>>(infoList, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:根据品牌id查询
	 * @param request
	 * @param brandId
	 * @return
	 * JsonResult<List<ProductCategoryVo>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月31日 下午12:49:34
	 */
	@RequestMapping(value="/selectByBrandId")
	@ResponseBody
	public JsonResult<Map<String, Object>> selectByBrandId(HttpServletRequest request,Integer brandId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(brandId==null) {
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		HashedMap<String, Object> resultMap = new HashedMap<String, Object>();
		BrandInfo brandInfo = new BrandInfo();
		brandInfo.setBrandStatus(BrandStatus.STATUS_1.getCode());
		brandInfo.setId(brandId);
		List<BrandInfoVo> brandList = brandInfoService.selectBrandList(brandInfo);
		BrandInfoVo brandInfoVo = brandList.get(0);
		String logo = brandInfoVo.getBrandLogo();
		if(StringUtils.isNoneBlank(logo)) {
			brandInfoVo.setBrandLogo(GlobalConfig.FILE_SERVICE_LOCATION+logo);
		}	
		resultMap.put("currentBrandList", brandInfoVo);
			
		ProductCategoryQuery categoryQuery = new ProductCategoryQuery();
		categoryQuery.setCategoryStatus(CategoryStatus.STATUS_1.getCode());
		categoryQuery.setBrandId(brandId);
		List<ProductCategoryVo> currentSubCategory = productCategoryService.selectList(categoryQuery);
		if(currentSubCategory.size()>0) {
			for (ProductCategoryVo pc : currentSubCategory) {
				String appletIcon = pc.getAppletIcon();
				if(StringUtils.isNoneBlank(appletIcon)) {
					pc.setAppletIcon(GlobalConfig.FILE_SERVICE_LOCATION+appletIcon);
				}	
			}
		}
		resultMap.put("currentSubCategory", currentSubCategory);
		return new JsonResult<Map<String, Object>>(resultMap, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:根据类别id查询
	 * @param request
	 * @param categoryId
	 * @return
	 * JsonResult<Map<String,Object>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月11日 下午11:30:30
	 */
	@RequestMapping(value="/selectByCategoryId")
	@ResponseBody
	public JsonResult<Map<String, Object>> selectByCategoryId(HttpServletRequest request,Integer categoryId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(categoryId==null) {
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		HashedMap<String, Object> resultMap = new HashedMap<String, Object>();
		ProductCategory currentCategory = productCategoryService.getById(categoryId);
		if(currentCategory!=null) {
			String icon = currentCategory.getAppletIcon();
			if(StringUtils.isNoneBlank(icon)) {
				currentCategory.setAppletIcon(GlobalConfig.FILE_SERVICE_LOCATION+icon);
			}	
		}
		resultMap.put("currentCategory", currentCategory);
		Integer brandId = currentCategory.getBrandId();
		ProductCategoryQuery categoryQuery = new ProductCategoryQuery();
		categoryQuery.setCategoryStatus(CategoryStatus.STATUS_1.getCode());
		categoryQuery.setBrandId(brandId);
		List<ProductCategoryVo> brotherCategory = productCategoryService.selectList(categoryQuery);
		if(brotherCategory.size()>0) {
			for (ProductCategoryVo pc : brotherCategory) {
				String appletIcon = pc.getAppletIcon();
				if(StringUtils.isNoneBlank(appletIcon)) {
					pc.setAppletIcon(GlobalConfig.FILE_SERVICE_LOCATION+appletIcon);
				}	
			}
		}
		resultMap.put("brotherCategory", brotherCategory);
		
		BrandInfo brandInfo = new BrandInfo();
		brandInfo.setBrandStatus(BrandStatus.STATUS_1.getCode());
		brandInfo.setId(brandId);
		List<BrandInfoVo> brandList = brandInfoService.selectBrandList(brandInfo);
		BrandInfoVo brandInfoVo = null;
		if(brandList.size()>0) {	
			brandInfoVo = brandList.get(0);
			String icon = currentCategory.getAppletIcon();
			if(StringUtils.isNoneBlank(icon)) {
				currentCategory.setAppletIcon(GlobalConfig.FILE_SERVICE_LOCATION+icon);
			}	
		}
		resultMap.put("parentBrand", brandInfoVo);
		
		return new JsonResult<Map<String, Object>>(resultMap, HttpCode.SUCCESS);
	}
}
